package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.example.demo.dao.BookMapper;
import com.example.demo.dao.ChapterMapper;
import com.example.demo.model.Book;
import com.example.demo.model.Chapter;
import com.example.demo.model.User;

import com.example.demo.service.HistoryService;


import com.example.demo.service.NLPService;
import com.example.demo.service.TextService;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 功能描述：
 *访问小说阅读页面
 * @Author: Zhanghf
 * @Date: 2021/5/21 15:37
 */
@RestController
public class TextController {
    @Autowired(required = false)
    private TextService textService;
    @Autowired(required = false)
    private NLPService nlpService;
    @Autowired(required = false)
private BookMapper bookMapper;

//    测试用：https://blog.csdn.net/CoderYin/article/details/90754547
    @GetMapping("getlisttest")
    public List<Book> getlisttest(Integer bookid) throws IOException {
        List<Book> books = bookMapper.selectList(new QueryWrapper<Book>().le("id", bookid));
        return books;
    }

    @GetMapping("gettext")
    public String gettext(Integer bookid,Integer chapterid) throws IOException {
        return textService.gettext(bookid, chapterid);
    }
    @GetMapping("gettitle")
    public String gettitle(Integer bookid,Integer chapterid) throws IOException {
        return textService.gettitle(bookid, chapterid);
    }

//    针对某一章节进行角色情感分析
    @PostMapping("getEmotionAnalysis")
    public Map<String, Integer> getEmotionAnalysis(@RequestParam("content")String content, @RequestParam("rolename") String rolename) throws IOException
    {
        Map<String, Integer> integerStringMap = nlpService.analysisPage(content, rolename);
        return integerStringMap;
    }

    @PostMapping("gethighFrequencyForString")
    public Map<String, Float> GethighFrequencyForString(@RequestParam("content")String content, @RequestParam("size") Integer size) throws IOException
    {
        Map<String, Float> stringFloatMap = nlpService.highFrequencyForString(content, size);
        return stringFloatMap;
    }

    @PostMapping("gethighFrequencyForNovel")
    public Map<String, Float> GethighFrequencyForNovel(@RequestParam("bookid")Integer bookid, @RequestParam("startsize") Integer startsize,@RequestParam("offsize") Integer offsize, @RequestParam("size") Integer size) throws IOException
    {
        Map<String, Float> stringFloatMap = nlpService.highFrequencyForNovel(bookid, startsize, offsize, size);
        return stringFloatMap;
    }

    @GetMapping("getLeadingRole")
    public String getLeadingRole(@RequestParam("bookid")Integer bookid) throws IOException
    {
        String leadingRole = nlpService.getLeadingRole(bookid);
        return leadingRole;
    }


}
